#include "mysql_chat_session.hpp"
#include "mysql_apply.hpp"
#include "mysql_relation.hpp"
#include <gflags/gflags.h>

// 定义命令行参数
DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

// ========================== RelationTable 测试 ==========================
void r_insert_test(mag::RelationTable &tb) {
    std::cout << "[RelationTable Insert] 开始插入好友关系" << std::endl;
    if (tb.insert("用户ID1", "用户ID2"))
        std::cout << "插入 用户ID1 <-> 用户ID2 成功" << std::endl;
    else
        std::cout << "插入 用户ID1 <-> 用户ID2 失败" << std::endl;

    if (tb.insert("用户ID1", "用户ID3"))
        std::cout << "插入 用户ID1 <-> 用户ID3 成功" << std::endl;
    else
        std::cout << "插入 用户ID1 <-> 用户ID3 失败" << std::endl;
}

void r_select_test(mag::RelationTable &tb) {
    std::cout << "[RelationTable Select] 查询 用户ID1 的好友列表：" << std::endl;
    auto res = tb.friends("用户ID1");
    if (res.empty()) {
        std::cout << "用户ID1 没有好友" << std::endl;
    } else {
        for (auto &uid : res) {
            std::cout << "好友ID: " << uid << std::endl;
        }
    }
}

void r_remove_test(mag::RelationTable &tb) {
    std::cout << "[RelationTable Remove] 删除 用户ID1 和 用户ID2 的好友关系" << std::endl;
    if (tb.remove("用户ID1", "用户ID2"))
        std::cout << "删除成功" << std::endl;
    else
        std::cout << "删除失败" << std::endl;
}

void r_exists_test(mag::RelationTable &tb) {
    std::cout << "[RelationTable Exists] 检查好友关系存在性" << std::endl;
    std::cout << "用户ID1 和 用户ID2: " << tb.exists("用户ID1", "用户ID2") << std::endl;
    std::cout << "用户ID1 和 用户ID3: " << tb.exists("用户ID1", "用户ID3") << std::endl;
}

// ========================== FriendApplyTable 测试 ==========================
void a_insert_test(mag::FriendApplyTable &tb) {
    std::cout << "[FriendApplyTable Insert] 插入好友申请" << std::endl;
    mag::FriendApply fa1("uuid1", "用户ID1", "用户ID2");
    if (tb.insert(fa1))
        std::cout << "好友申请 (用户ID1 -> 用户ID2) 插入成功" << std::endl;
    else
        std::cout << "好友申请 (用户ID1 -> 用户ID2) 插入失败" << std::endl;
}

void a_select_test(mag::FriendApplyTable &tb) {
    std::cout << "[FriendApplyTable Select] 查询 用户ID2 的申请者" << std::endl;
    auto res = tb.applyUsers("用户ID2");
    if (res.empty()) {
        std::cout << "用户ID2 没有收到任何好友申请" << std::endl;
    } else {
        for (auto &uid : res) {
            std::cout << "申请者ID: " << uid << std::endl;
        }
    }
}

void a_remove_test(mag::FriendApplyTable &tb) {
    std::cout << "[FriendApplyTable Remove] 删除 用户ID1 -> 用户ID2 的好友申请" << std::endl;
    if (tb.remove("用户ID1", "用户ID2"))
        std::cout << "删除成功" << std::endl;
    else
        std::cout << "删除失败" << std::endl;
}

// ========================== ChatSessionTable 测试 ==========================
void c_insert_test(mag::ChatSessionTable &tb) {
    std::cout << "[ChatSessionTable Insert] 插入会话" << std::endl;
    mag::ChatSession cs1("会话ID1", "单聊会话", mag::ChatSessionType::SINGLE);
    if (tb.insert(cs1))
        std::cout << "单聊会话 插入成功" << std::endl;
    else
        std::cout << "单聊会话 插入失败" << std::endl;
}

void c_select_test(mag::ChatSessionTable &tb) {
    std::cout << "[ChatSessionTable Select] 查询会话ID1" << std::endl;
    auto res = tb.select("会话ID1");
    if (res) {
        std::cout << "会话ID: " << res->chat_session_id() << std::endl;
        std::cout << "会话名称: " << res->chat_session_name() << std::endl;
    } else {
        std::cout << "会话ID1 不存在" << std::endl;
    }
}

void c_remove_test(mag::ChatSessionTable &tb) {
    std::cout << "[ChatSessionTable Remove] 删除会话ID1" << std::endl;
    if (tb.remove("会话ID1"))
        std::cout << "删除成功" << std::endl;
    else
        std::cout << "删除失败" << std::endl;
}

// ========================== 主函数 ==========================
int main(int argc, char *argv[]) {
    // 解析命令行参数
    google::ParseCommandLineFlags(&argc, &argv, true);
    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    // 数据库连接初始化
    auto db = mag::ODBFactory::create("root", "123456", "127.0.0.1", "mag", "utf8", 0, 1);

    // 表实例化
    mag::RelationTable rtb(db);
    mag::FriendApplyTable fatb(db);
    mag::ChatSessionTable cstb(db);

    // ================== 逐步测试各个功能 ==================
    std::cout << "===== RelationTable 测试 =====" << std::endl;
    r_insert_test(rtb);
    r_select_test(rtb);
    r_exists_test(rtb);
    r_remove_test(rtb);
    r_select_test(rtb);

    std::cout << "\n===== FriendApplyTable 测试 =====" << std::endl;
    a_insert_test(fatb);
    a_select_test(fatb);
    a_remove_test(fatb);
    a_select_test(fatb);

    std::cout << "\n===== ChatSessionTable 测试 =====" << std::endl;
    c_insert_test(cstb);
    c_select_test(cstb);
    c_remove_test(cstb);
    c_select_test(cstb);

    return 0;
}
